﻿'
' Quiz Module for DotNetNuke
' Project Contributors - Will Strohl (http://www.WillStrohl.com)
'
'Copyright (c) 2011, Will Strohl
'All rights reserved.
'
'Redistribution and use in source and binary forms, with or without modification, are 
'permitted provided that the following conditions are met:
'
'Redistributions of source code must retain the above copyright notice, this list of 
'conditions and the following disclaimer.
'
'Redistributions in binary form must reproduce the above copyright notice, this list 
'of conditions and the following disclaimer in the documentation and/or other 
'materials provided with the distribution.
'
'Neither the name of Quiz Module for DotNetNuke, nor the names of its contributors may be 
'used to endorse or promote products derived from this software without specific prior 
'written permission.
'
'THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY 
'EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 
'OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT 
'SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 
'INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED 
'TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR 
'BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 
'CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN 
'ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH 
'DAMAGE.
'

Imports DotNetNuke.Entities.Icons
Imports DotNetNuke.Entities.Icons.IconController
Imports DotNetNuke.Security
Imports DotNetNuke.Services.Exceptions
Imports DotNetNuke.UI.Skins.Skin
Imports WillStrohl.Modules.Quiz.QuizController
Imports WillStrohl.Modules.Quiz.QuizInfoMembers

Namespace WillStrohl.Modules.Quiz

    Partial Public MustInherit Class QuizList
        Inherits WNSPortalModuleBase

#Region " Private Members "

        Private Const ICON_FORMAT As String = "<img src=""{0}"" alt=""{1}"" title=""{1}"" />"
        Private Const ICON_LINK_FORMAT As String = "<a href=""{0}""><img src=""{1}"" alt=""{2}"" title=""{2}"" /></a>"

        Private p_ListAction As String = Null.NullString
        Private p_QuizId As Integer = Null.NullInteger

#End Region

#Region " Properties "

        Private ReadOnly Property ListAction() As String
            Get
                If String.IsNullOrEmpty(p_ListAction) Then
                    Dim oAction As Object = Request.Params(QUERYSTRING_ACTION_KEY)
                    If Not oAction Is Nothing Then
                        p_ListAction = oAction.ToString()
                    End If
                End If

                Return p_ListAction
            End Get
        End Property

        Private ReadOnly Property QuizId() As Integer
            Get
                If p_QuizId = Null.NullInteger Then
                    Dim oSetting As Object = Request.Params(QUERYSTRING_QUIZ_ID_KEY)
                    If Not oSetting Is Nothing Then
                        p_QuizId = Integer.Parse(oSetting.ToString(), Globalization.NumberStyles.Integer)
                    End If
                End If

                Return p_QuizId
            End Get
        End Property

#End Region

#Region " Event Handlers "

        Private Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
            Try

                If Not Page.IsPostBack Then
                    Me.BindData()
                End If

            Catch exc As Exception ' Module failed to load
                ProcessModuleLoadException(Me, exc, Me.IsEditable)
            End Try
        End Sub

        Private Sub lnkReturn_Click(sender As Object, e As System.EventArgs) Handles lnkReturn.Click
            SendToDefaultView()
        End Sub

#End Region

#Region " Private Helper Functions "

        Private Sub BindData()

            ProcessActions()

            Me.LocalizeModule()

            Dim ctlQuiz As New QuizController
            Dim collQuiz As QuizInfoCollection = ctlQuiz.GetQuizzes(PortalId)

            rptQuizList.DataSource = collQuiz
            rptQuizList.DataBind()

        End Sub

        Private Sub LocalizeModule()

            lnkReturn.Text = GetLocalizedString("lnkReturn.Text")

        End Sub

        Private Sub ProcessActions()

            If Not String.IsNullOrEmpty(ListAction) Then

                Select Case ListAction.ToLower
                    Case "assign"
                        Dim ctlModule As New Entities.Modules.ModuleController
                        ctlModule.UpdateTabModuleSetting(TabModuleId, QUIZ_ID_FIELD, QuizId.ToString())
                        Entities.Modules.ModuleController.SynchronizeModule(ModuleId)
                    Case "delete"
                        Dim ctlQuiz As New QuizController
                        ctlQuiz.DeleteQuiz(QuizId)
                End Select

                ReloadPage()

            End If

        End Sub

        Protected Function GetUserDisplayName(ByVal GridUserId As Object) As String

            Dim ctlUser As New Entities.Users.UserController
            Dim oUser As UserInfo = ctlUser.GetUser(PortalId, Integer.Parse(GridUserId.ToString(), Globalization.NumberStyles.Integer))

            If Not oUser Is Nothing Then
                Return oUser.DisplayName
            Else
                Return String.Format("&lt;{0}&gt;", GetLocalizedString("Error.UserNotFound"))
            End If

        End Function

        Protected Function GetDateFormat(ByVal GridDate As Object) As String

            Dim dteValue As DateTime = DateTime.Parse(GridDate.ToString())
            Return dteValue.ToString("MM/dd/yyyy hh:mm tt")

        End Function

        Protected Function GetEditIcon(ByVal GridQuizId As Object) As String

            Dim strIcon As String = Entities.Icons.IconController.IconURL("Edit", DefaultIconSize, DefaultIconStyle)
            Return String.Format(ICON_LINK_FORMAT, _
                EditUrl(String.Empty, String.Empty, "Edit", String.Concat(QUERYSTRING_QUIZ_ID_KEY, "=", GridQuizId.ToString())), _
                strIcon, _
                GetLocalizedString("Link.Edit"))

        End Function

        Protected Function GetDeleteIcon(ByVal GridQuizId As Object) As String

            Dim strIcon As String = Entities.Icons.IconController.IconURL("Delete", DefaultIconSize, DefaultIconStyle)
            Return String.Format(ICON_LINK_FORMAT, _
                EditUrl(String.Empty, String.Empty, "List", String.Format("{0}={1}&{2}={3}", QUERYSTRING_QUIZ_ID_KEY, GridQuizId.ToString(), QUERYSTRING_ACTION_KEY, "Delete")), _
                strIcon, _
                GetLocalizedString("Link.Delete"))

        End Function

        Protected Function GetQuestionIcon(ByVal GridQuizId As Object) As String

            Dim strIcon As String = Entities.Icons.IconController.IconURL("ExtChm", DefaultIconSize, DefaultIconStyle)
            Return String.Format(ICON_LINK_FORMAT, _
                EditUrl(String.Empty, String.Empty, "Questions", String.Concat(QUERYSTRING_QUIZ_ID_KEY, "=", GridQuizId.ToString())), _
                strIcon, _
                GetLocalizedString("Link.Question"))

        End Function

        Protected Function GetAssignmentIcon(ByVal GridQuizId As Object) As String

            Dim oQuizId As Object = Settings(QUIZ_ID_FIELD)

            If Not oQuizId Is Nothing AndAlso String.Equals(oQuizId.ToString(), GridQuizId.ToString()) Then
                Dim strAssignedIcon As String = Entities.Icons.IconController.IconURL("Checked", DefaultIconSize, DefaultIconStyle)

                Return String.Format(ICON_FORMAT, _
                    strAssignedIcon, _
                    GetLocalizedString("Link.Assigned"))
            Else
                Dim strUnassignedIcon As String = Entities.Icons.IconController.IconURL("Unchecked", DefaultIconSize, DefaultIconStyle)

                Return String.Format(ICON_LINK_FORMAT, _
                    EditUrl(String.Empty, String.Empty, "List", String.Format("{0}={1}&{2}={3}", QUERYSTRING_QUIZ_ID_KEY, GridQuizId.ToString(), QUERYSTRING_ACTION_KEY, "Assign")), _
                    strUnassignedIcon, _
                    GetLocalizedString("Link.Unassigned"))
            End If

        End Function

        Protected Function GetQuestionCount(ByVal GridQuizId As Object) As String

            Dim intQuizId As Integer = Integer.Parse(GridQuizId.ToString(), Globalization.NumberStyles.Integer)
            Dim ctlQuiz As New QuizController

            Return ctlQuiz.GetQuestionCount(intQuizId).ToString()

        End Function

        Private Sub SendToDefaultView()
            Response.Redirect(NavigateURL)
        End Sub

        Private Sub ReloadPage()
            Response.Redirect(EditUrl(String.Empty, String.Empty, "List"))
        End Sub

        Private Sub HandleException(ByVal exc As Exception)
            LogException(exc)
            If Me.UserInfo.IsSuperUser Or Me.UserInfo.UserID = PortalSettings.AdministratorId Then
                AddModuleMessage(Me, _
                    String.Concat(exc.Message, "<br />", exc.StackTrace), _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            Else
                AddModuleMessage(Me, exc.Message, _
                    Skins.Controls.ModuleMessage.ModuleMessageType.RedError)
            End If
        End Sub

#End Region
        
    End Class

End Namespace